﻿@inherits ProComponentBase
@page "/job"

<PageTitle>@T("Job")</PageTitle>
<AutoHeight Overflow="_jobs.Any()" AutoClass="relative mt-3">
    <HeaderContent>
        <MRow NoGutters Class="flex-none mt-3">
            <MCol Md="2">
                <Projects OnProjectChanged="OnProjectChangedAsync" TeamId="_teamId" ProjectIdentity="@_projectIdentity">
                </Projects>
            </MCol>
            
            <SLabeledRadioGroup Value="_jobCreateType" ActiveClass="fill-background" Class="white ml-6" Dense TValue="JobCreateTypes" ValueChanged="SwitchJobCreateType">
                @foreach (var item in Enum.GetValues<JobCreateTypes>())
                {
                    <SLabeledRadio Value="item" Class="px-6">
                        <span class="@(_jobCreateType == item ? "subtitle2 emphasis2--text" : "btn regular2--text")">@T(nameof(JobCreateTypes)+"."+item)</span>
                    </SLabeledRadio>
                }
            </SLabeledRadioGroup>
            <MSpacer></MSpacer>
            <div class="d-flex">
                <SSearch @bind-Value="QueryJobName" BackgroundColor="white" />
                <div @onclick="ToggleAdvanced" class="ml-6 block-center justify-center button fill">
                    <SIcon Class="ma-auto" Color="emphasis2" Size=20 Tooltip="@(!_advanced ? T("Filter") : T("CancelFilter"))">
                        @(!_advanced ? "mdi-filter" : "mdi-filter-off")
                    </SIcon>
                </div>
                @if (_jobCreateType == JobCreateTypes.Manual)
                {
                    <SButton Small BorderRadiusClass="rounded-lg" Class="ml-6" OnClick="AddJob">
                        <SIcon Size=18 IsDefaultToolTip=false>@IconConstants.Add</SIcon>
                        <span class="ml-2 btn">@T("Add")</span>
                    </SButton>
                }
            </div>
        </MRow>
        @if (_advanced)
        {
            <MRow Class="@($"mt-6 flex-none")" NoGutters>
                <MCol Md="2">
                    <SSelect Small @bind-Value="QueryTimeType"
                             Items="_jobQueryTimeTypeList"
                             Label="@T("JobQueryTimeType")"
                             ItemText="e => T(e == default ? string.Empty : e.ToString())"
                             ItemValue="e => e"
                             BackgroundColor="white"
                             Class="rounded-2 body2">
                    </SSelect>
                </MCol>
                <SDateTimeRangeToolbar class="ml-6" ShowQuickChange="false" StartDateTime=_queryStartTime EndDateTime=_queryEndTime OnUpdate="QueryTimeChanged" Clearable></SDateTimeRangeToolbar>
                <MCol Md="2" Class="ml-6">
                    <SSelect Small @bind-Value="QueryJobType"
                         Items="Enum.GetValues<JobTypes>().ToList()"
                         Label="@T("JobType")"
                         ItemText="e => T(e==default?string.Empty:e.ToString())"
                         ItemValue="e=> e"
                         BackgroundColor="white"
                              Clearable
                         Class="rounded-2 body2">
                    </SSelect>
                    </MCol>
                    @if (_jobCreateType == JobCreateTypes.Api)
                {
                    <MCol Md="2" Class="ml-6">
                        <SSelect Small Clearable @bind-Value="QueryOrigin"
                         Items="_originList"
                         Label="@T("Origin")"
                         ItemText="e => e"
                         ItemValue="e=> e"
                         BackgroundColor="white"
                         Class="rounded-2 body2">
                        </SSelect>
                    </MCol>
                }
            </MRow>
        }
        <MRadioGroup @bind-Value="QueryStatus" Row OnClick="RadioGroupClickHandler" Class="masa-radiogroup">
            @foreach (KeyValuePair<string, TaskRunStatus> pair in _queryStatusList)
            {
                <MRadio TValue="TaskRunStatus" Value="pair.Value" Color="green">
                    <LabelContent>
                        <span>@T(pair.Key)</span>
                    </LabelContent>
                </MRadio>
            }
        </MRadioGroup>
    </HeaderContent>
    <AutoHeightContent>
        @if (_jobs.Any())
        {
            <MRow>
                @foreach (var job in _jobs)
                {
                    <MCol Md="6">
                        <MCard Height="115" Class="pt-2" OnClick="()=> HandleJobSelect(job)" Style="@($"border-radius:10px !important; {(job.Enabled?"":"opacity: 0.6")}")">
                            <MCardTitle>
                                <MChip Class="job-text btn"
                                    TextColor="emphasis2"
                                    Style="height:25px; background-color:#F0F3FA"
                                        Label>
                                    @T(job.JobType.ToString())
                                </MChip>
                                <div class="job-text job-name text-truncate emphasis2--text h7 ml-2">
                                    @job.Name
                                </div>
                            </MCardTitle>
                            <MCardText Class="d-flex height-inherit">
                                <SButton Icon XSmall Color="@GetJobColor(job)" Class="run-icon" Style="cursor:default">
                                    <SIcon IsDefaultToolTip=false>mdi-checkbox-blank-circle</SIcon>
                                </SButton>
                                <div class="job-text run-text pl-2 body2" style="color:@(GetJobColor(job))">
                                    @GetJobRunText(job)
                                </div>
                                <div class="d-flex px-1 mt-2" style="height: 8px">
                                    <MDivider Vertical />
                                </div>
                                <div class="body2 regular3--text">@job.ModificationTime.Humanize(culture:I18n.Culture) </div>
                                <MSpacer></MSpacer>
                                @if (_jobCreateType == JobCreateTypes.Api)
                                {
                                    <MChip Class="job-text emphasis2--text btn"
                                        Style="height:25px; background-color:#F0F3FA">
                                        @job.Origin
                                    </MChip>
                                }
                                @if (!string.IsNullOrWhiteSpace(job.UserName))
                                {
                                    <MTooltip Top>
                                        <ActivatorContent>
                                            <MAvatar Size="24" @attributes="@context.Attrs" Class="ml-4">
                                                <MImage Src="@job.Avator"></MImage>
                                            </MAvatar>
                                        </ActivatorContent>
                                        <ChildContent>
                                            <span>@T("Owner"): @job.UserName</span>
                                        </ChildContent>
                                    </MTooltip>
                                }
                                else
                                {
                                    <MTooltip>
                                        <ActivatorContent>
                                            <MAvatar Size="24" @attributes="@context.Attrs" Class="ml-4">
                                                <MImage Src=""></MImage>
                                            </MAvatar>
                                        </ActivatorContent>
                                        <ChildContent>
                                            <span></span>
                                        </ChildContent>
                                    </MTooltip>
                                }
                                <MMenu Bottom Left>
                                    <ActivatorContent>
                                        <SButton Color="" Icon @attributes="@context.Attrs" Width="24" Height="24" OnClickStopPropagation Class="ml-4">
                                            <SIcon>mdi-dots-vertical</SIcon>
                                        </SButton>
                                    </ActivatorContent>
                                    <ChildContent>
                                        <MList>
                                            @if (job.Enabled)
                                            {
                                                <MListItem Link OnClick="()=> RunJob(job)">
                                                    <MListItemContent Class="pl-6 pr-16">
                                                        <MListItemTitle>
                                                <SIcon Color="#323D6F" Class="align-self-center mr-3" IsDefaultToolTip="false">fa:fas fa-spinner</SIcon>
                                                            @T("Run")
                                                        </MListItemTitle>
                                                    </MListItemContent>
                                                </MListItem>
                                            }
                                            <MListItem Link OnClick="()=> EditJob(job)">
                                                <MListItemContent Class="pl-6 pr-16">
                                                    <MListItemTitle>
                                                        <SIcon Color="#323D6F" Class="align-self-center mr-3" IsDefaultToolTip="false">mdi-pencil</SIcon>
                                                        @T("Edit")
                                                    </MListItemTitle>
                                                </MListItemContent>
                                            </MListItem>
                                            @if (job.Enabled)
                                            {
                                                <MListItem Link OnClick="()=> ShowDialog(ConfirmDialogTypes.DisabledJob, job.Id)">
                                                    <MListItemContent Class="pl-6 pr-16">
                                                        <MListItemTitle>
                                                            <SIcon Color="red" Class="align-self-center mr-3" IsDefaultToolTip="false">mdi-minus-circle</SIcon>
                                                            @T("Disabled")
                                                        </MListItemTitle>
                                                    </MListItemContent>
                                                </MListItem>
                                            }
                                            else
                                            {
                                                <MListItem Link OnClick="()=> ShowDialog(ConfirmDialogTypes.EnabledJob, job.Id)">
                                                    <MListItemContent Class="pl-6 pr-16">
                                                        <MListItemTitle>
                                                            <SIcon Color="green" Class="align-self-center mr-3">mdi-check-circle</SIcon>
                                                            @T("Enabled")
                                                        </MListItemTitle>
                                                    </MListItemContent>
                                                </MListItem>
                                            }
                                        </MList>
                                    </ChildContent>
                                </MMenu>
                            </MCardText>
                        </MCard>
                    </MCol>
                }
            </MRow>
        }
        else if(!_showProgressbar)
        {
            <SEmptyPlaceholder />
        }
        <Loading ShowProgressbar="_showProgressbar" />
    </AutoHeightContent>
    <FooterContent>
        @if (_jobs.Any())
        {
            <SPagination Class="mr-6 pb-2" @bind-Page="Page" @bind-PageSize=PageSize Total="_total" />
        }        
    </FooterContent>
</AutoHeight>

<JobModal @ref="_jobModal" Project="Project" OnAfterDataChange="OnAfterDataChange"></JobModal>
<SSimpleDialog OkText="@T("Sure")" OnOk="OnSure" @bind-Value="_showConfirmDialog" Title="@_confirmTitle" HideCancel OkColor="#FF5252" OkClass="full-width scheduler-confirm-dialog-ok-button">
    <ChildContent>
        <span class="d-flex flex-column justify-center align-center" style="text-align:center">
            @_confirmMessage
        </span>
    </ChildContent>
</SSimpleDialog>
